package com.snowy.hidden.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

import java.util.UUID;

/**
 * @Description: traceId管理工具类
 * @Author yanglei
 * @Date 2022/5/6 17:24
 * @Version 1.0
 */
@Slf4j
public class TraceUtils {
    private static final String TRACE_ID = "traceId";

    /**
     * 添加traceId
     */
    public static void createTraceId() {
        String traceId = MDC.get(TRACE_ID);
        if (StringUtils.isBlank(traceId)) {
            traceId = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
            log.debug("create traceId :{}", traceId);
            MDC.put(TRACE_ID, traceId);
        }
    }

    /**
     * 销毁traceId
     */
    public static void destroyTraceId() {
        MDC.remove(TRACE_ID);
    }


}
